<template>
  <div class="app-container">
    <h2 class="data-list-title" style=" margin-bottom: 5px; color: #6495ED; font-family: 'Roboto', 'Arial Narrow', sans-serif; font-weight: 500;">
      <i class="el-icon-search"></i>
        房间及图片信息
    </h2>
    <div style="border-bottom: 2px solid #6495ED;margin-top: 0px;margin-bottom: 10px;"></div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['romimgview:romimgview:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['romimgview:romimgview:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['romimgview:romimgview:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['romimgview:romimgview:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="romimgviewList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房间ID" align="center" prop="roomId" />
      <el-table-column label="房间名称" align="center" prop="roomName" />
      <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="区/县" align="center" prop="county" />
      <el-table-column label="具体位置" align="center" prop="roomLocation" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="room_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="月租" align="center" prop="monthlyRent" >
        <template #default="scope">
          <div>
            {{ scope.row.monthlyRent !== null ? '¥' + scope.row.monthlyRent : '' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="图片ID" align="center" prop="imageId" />
      <el-table-column label="图片名称" align="center" prop="imageName" />
      <el-table-column label="文件路径" align="center" prop="filePath" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.filePath" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['romimgview:romimgview:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['romimgview:romimgview:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <el-divider content-position="center" style="margin-bottom: 50px;"></el-divider>
    <h2 class="data-list-title" style=" margin-bottom: 5px;margin-top: 0; color: #6495ED; font-family: 'Roboto', 'Arial Narrow', sans-serif; font-weight: 500;">
      <i class="el-icon-search"></i>
        搜索条件
    </h2>
    <div style="border-bottom: 2px solid #6495ED;margin-top: 0px;margin-bottom: 10px;"></div>
    
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px" label-position="left">
      <el-divider content-position="center" style="margin: 0px 0;"></el-divider>
      <h3 style="color: #6495ED; font-family: 'Roboto', 'Arial Narrow', sans-serif; font-weight: 400;">
        房间搜索条件：
      </h3>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="房间ID" prop="roomId">
            <el-input
              v-model="queryParams.roomId"
              placeholder="请输入房间ID"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="房间名称" prop="roomName">
            <el-input
              v-model="queryParams.roomName"
              placeholder="请输入房间名称"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        
        <el-col :span="8">
          <el-form-item label="省份" prop="province">
            <el-select v-model="queryParams.province" placeholder="请选择省份" clearable @change="changeCityListQuery($event)">
              <el-option
                v-for="province in provinceListQuery"
                :key="province.id"
                :label="province.name"
                :value="province.name"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="城市" prop="city">
            <el-select v-model="queryParams.city" placeholder="请选择城市" clearable @change="changeCountyListQuery($event)" :disabled="!queryParams.province">
              <el-option
                v-for="city in cityListQuery"
                :key="city.id"
                :label="city.name"
                :value="city.name"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="区/县" prop="county">
            <el-select v-model="queryParams.county" placeholder="请选择区/县" clearable :disabled="!queryParams.city">
              <el-option
                v-for="county in countyListQuery"
                :key="county.id"
                :label="county.name"
                :value="county.name"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        
        <el-col :span="8">
          <el-form-item label="具体位置" prop="roomLocation">
            <el-input
              v-model="queryParams.roomLocation"
              placeholder="请输入房间具体位置"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="房间状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
              <el-option
                v-for="dict in room_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="月租" prop="monthlyRent">
            <el-input
              v-model="queryParams.monthlyRent"
              placeholder="请输入月租"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-divider content-position="center" style="margin: 0px 0;"></el-divider>
      <h3 style="color: #6495ED; font-family: 'Roboto', 'Arial Narrow', sans-serif; font-weight: 400;">
        图片搜索条件：
      </h3>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="图片ID" prop="imageId">
            <el-input
              v-model="queryParams.imageId"
              placeholder="请输入图片ID"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="图片名称" prop="imageName">
            <el-input
              v-model="queryParams.imageName"
              placeholder="请输入图片名称"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="描述" prop="description">
            <el-input
              v-model="queryParams.description"
              placeholder="请输入描述"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-divider content-position="center" style="margin-bottom: 50px;"></el-divider>
    
    
    <!-- 添加或修改房间图片管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="romimgviewRef" :model="form" :rules="rules" label-width="80px">
        <el-divider content-position="center">房间信息</el-divider>
        <el-form-item label="房间ID" prop="roomId" @change="getRoom()" >
          <el-input v-model="form.roomId" placeholder="请输入房间ID" />
        </el-form-item>
        <el-form-item label="房间名称" prop="roomName">
          <span>{{ form.roomName }}</span>
        </el-form-item>
        
        <el-form-item label="省份" prop="province">
          <span>{{ form.province }}</span>
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <span>{{ form.city }}</span>
        </el-form-item>
        <el-form-item label="区/县" prop="county">
          <span>{{ form.county }}</span>
        </el-form-item>
        
        <el-form-item label="房间具体位置" prop="roomLocation">
          <span>{{ form.roomLocation }}</span>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <dict-tag :options="room_status" :value="form.status"/>
        </el-form-item>
        <el-form-item label="月租" prop="monthlyRent">
          <span>{{ form.monthlyRent }}</span>
        </el-form-item>
        <el-divider content-position="center">房间对应图片信息</el-divider>
        <el-form-item label="图片ID" prop="imageId" @change="getImage()">
          <el-input v-model="form.imageId" placeholder="请输入图片ID" />
        </el-form-item>
        <el-form-item label="图片名称" prop="imageName">
          <span>{{ form.imageName }}</span>
        </el-form-item>
        <el-form-item label="文件路径" prop="filePath">
          <image-preview :src="form.filePath" :width="100" :height="100"/>
        </el-form-item>
        <el-form-item label="图片描述" prop="description">
          <span>{{ form.description }}</span>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFormAdd" v-show="showAdd">确定新增</el-button>
          <el-button type="primary" @click="submitFormUpdate" v-show="showUpdate">确定修改</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Romimgview">
import { listRomimgview, getRomimgview,getNewRoom,getNewImage, delRomimgview, addRomimgview, updateRomimgview } from "@/api/romimgview/romimgview";
const { proxy } = getCurrentInstance();
const { room_status } = proxy.useDict('room_status');
const romimgviewList = ref([]);
const open = ref(false);
const showAdd = ref(true);
const showUpdate = ref(true);
const show = ref(true);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    roomId: null,
    roomName: null,
    province: null,
    city: null,
    county: null,
    roomLocation: null,
    status: null,
    monthlyRent: null,
    imageId: null,
    imageName: null,
    filePath: null,
    description: null,
    imageRoomId: null
  },
  rules: {
    roomId: [
      { required: true, message: "房间ID不能为空", trigger: "blur" },
      { validator: (rule, value, callback) => {
          if (value && !/^\d+$/.test(value)) {
            callback(new Error('房间ID必须为数字'));
          } else {
            callback();
          }
        }, trigger: 'blur' 
      }
    ],
    imageId: [
      { required: true, message: "图片ID不能为空", trigger: "blur" },
      { validator: (rule, value, callback) => {
          if (value && !/^\d+$/.test(value)) {
            callback(new Error('图片ID必须为数字'));
          } else {
            callback();
          }
        }, trigger: 'blur' 
      }
    ]
  }
});
const { queryParams, form, rules } = toRefs(data);

/** 查询房间图片管理列表 */
function getList() {
  loading.value = true;
  listRomimgview(queryParams.value).then(response => {
    romimgviewList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    roomId: null,
    roomName: null,
    province: null,
    city: null,
    county: null,
    roomLocation: null,
    status: null,
    monthlyRent: null,
    imageId: null,
    imageName: null,
    filePath: null,
    description: null,
    imageRoomId: null
  };
  proxy.resetForm("romimgviewRef");
}



/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.imageRoomId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  showAdd.value = true;
  showUpdate.value = false;
  reset();
  open.value = true;
  title.value = "添加房间图片对应信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  showAdd.value = false;
  showUpdate.value = true;
  reset();
  const _imageRoomId = row.imageRoomId || ids.value;
  getRomimgview(_imageRoomId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改房间图片对应信息";

    // 在获取数据后初始化城市列表和区县列表
    getCityListByName(form.value.province);
    getCountyListByName(form.value.city);
  });
}

/** 提交按钮 */
function submitFormAdd() {
   proxy.$refs["romimgviewRef"].validate(valid => {
  if (valid) {
     addRomimgview(form.value).then(response => {
     proxy.$modal.msgSuccess("新增成功");
     open.value = false;
     getList();
   });
  }
 });
}

function submitFormUpdate() {
 proxy.$refs["romimgviewRef"].validate(valid => {
  if (valid) {
   updateRomimgview(form.value).then(response => {
    proxy.$modal.msgSuccess("修改成功");
    open.value = false;
    getList();   
    }); 
  }
 });
}


/** 删除按钮操作 */
function handleDelete(row) {
  const _imageRoomIds = row.imageRoomId || ids.value;
  proxy.$modal.confirm('是否确认删除房间图片管理编号为"' + _imageRoomIds + '"的数据项？').then(function() {
    return delRomimgview(_imageRoomIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('romimgview/romimgview/export', { ...queryParams.value }, `romimgview_${new Date().getTime()}.xlsx`);
}

getList();

//-----------------------------------------------------------------------
//对话框查询功能实现：
function resetRoom() {
  form.value = {
    roomId:form.value.roomId,
    roomName: null,
    province: null,
    city: null,
    county: null,
    roomLocation: null,
    status: null,
    monthlyRent: null,
    imageId:form.value.imageId,
    imageName: form.value.imageName,
    filePath: form.value.filePath,
    description: form.value.description,
    imageRoomId: form.value.imageRoomId
  };
  proxy.resetForm("romimgviewRef");
}

function resetImage() {
  form.value = {
    roomId:form.value.roomId,
    roomName: form.value.roomName,
    province: form.value.province,
    city: form.value.city,
    county: form.value.county,
    roomLocation: form.value.roomLocation,
    status: form.value.status,
    monthlyRent: form.value.monthlyRent,
    imageId:form.value.imageId,
    imageName: null,
    filePath: null,
    description: null,
    imageRoomId: form.value.imageRoomId
  };
  proxy.resetForm("romimgviewRef");
}

function getRoom(){
  resetRoom();
  if(form.value.roomId){
    getNewRoom(form.value.roomId).then(response => {
      form.value.roomName = response.data.roomName;
      form.value.province = response.data.province;
      form.value.city = response.data.city;
      form.value.county = response.data.county;
      form.value.roomLocation = response.data.roomLocation;
      form.value.status = response.data.status;
      form.value.monthlyRent = response.data.monthlyRent;
  });
}
}

function getImage(){
  resetImage();
  if(form.value.imageId){
    getNewImage(form.value.imageId).then(response => {
    form.value.imageName = response.data.imageName;
      form.value.filePath = response.data.filePath;
      form.value.description = response.data.description;
    });
  }
}
//-----------------------------------------------------------------------

//----------------------------------------
//省市县功能实现：
import { listTopCity, cityListByName } from "@/api/district/district";

const provinceList = ref([]);
const cityList = ref([]);
const countyList = ref([]);
/*
增加修改对话框省市县功能实现：
*/
// 获取增加修改对话框省份列表
function getProvinceList() {
  listTopCity().then(response => {
    provinceList.value = response.rows;
  });
}

// 增加修改对话框根据省份名字获取城市列表
function getCityListByName(provinceName){
  cityListByName({name:provinceName}).then(response=>{
    cityList.value = response.rows;
  })
}

// 增加修改对话框根据城市名字获取区县列表
function getCountyListByName(cityName){
  cityListByName({name:cityName}).then(response=>{
    countyList.value = response.rows;
  })
}

// 增加修改对话框改变城市列表
function changeCityList(provinceName){
  form.value.city = null;
  form.value.county = null;
  getCityListByName(provinceName);
}

// 增加修改对话框改变区县列表
function changeCountyList(cityName){
  form.value.county = null;
  getCountyListByName(cityName);
}

// 初始化获取增加修改对话框省份列表
getProvinceList();
// 初始化获取增加修改对话框城市列表和区县列表
getCityListByName(form.value.province);
// 初始化获取增加修改对话框区县列表
getCountyListByName(form.value.city);

/*
搜索框省市县功能实现：
*/
const provinceListQuery = ref([]);
const cityListQuery = ref([]);
const countyListQuery = ref([]);
// 获取搜索框省份列表
function getProvinceQueryList() {
  listTopCity().then(response => {
    provinceListQuery.value = response.rows;
  });
}

// 搜索框根据省份名字获取城市列表
function getCityListQueryByName(provinceName){
  
  cityListByName({name:provinceName}).then(response=>{
    cityListQuery.value = response.rows;  
  })
}
// 搜索框根据城市名字获取区县列表
function getCountyListQueryByName(cityName){
  //打印cityName
  
  cityListByName({name:cityName}).then(response=>{
    countyListQuery.value = response.rows;
  })
}

// 改变搜索框城市列表
function changeCityListQuery(provinceName){
  queryParams.value.city = null;
  queryParams.value.county = null;
  getCityListQueryByName(provinceName);
}
// 改变搜索框区县列表
function changeCountyListQuery(cityName){
  queryParams.value.county = null;
  getCountyListQueryByName(cityName);
}
// 初始化获取搜索框省份列表
getProvinceQueryList();
// 初始化获取搜索框城市列表
getCityListQueryByName(queryParams.value.province);
// 初始化获取搜索框区县列表
getCountyListQueryByName(queryParams.value.city);

//----------------------------------------
</script>
